// pages/wenda/wenda.js
//index.js
//获取应用实例
const app = getApp()
const util = require("../../utils/util.js");
Page({
    data: {
        menuindex: 0,
        // cityname: app.globalData.extConfig.cityname,
        // citycode: app.globalData.extConfig.citycode,
        showtk: false,
        path: '',
        city: "深圳",
        citycode: 'sz',
    },

    onLoad: function(opt) {
        this.setData({
            id: opt.id ? opt.id : '',
            type: opt.type ? opt.type : '',
            citycode: opt.city ? opt.city : (wx.getStorageSync("citycode") ? wx.getStorageSync("citycode") : this.data.citycode),
        })
        wx.setStorageSync('citycode', this.data.citycode)

        util.cityList().then((res) => {
            let citydata = res.allcity.filter(v => v.citycode == this.data.citycode);
            this.setData({
                city: citydata[0].cityname
            })
            wx.setStorageSync('city', this.data.city)
        })

        util.getUrl(app.globalData.url).then((res) => {
            this.getwendalist();
        });
    },

    // 问答频道
    getwendalist() {
        let data = {
            citycode: "wenda",
            action: "indexlist",
            city: wx.getStorageSync("city"),
            pagesize: 5,
            version: app.globalData.version,
        };
        util.request('index.php', data, 'GET').then(res => {
            this.setData({
                wendaData: res.data,
            })
            this.data.wendaData.forEach(v => { this.getToolData(v.wapname) })
        })
    },

    // 获取工具和专题接口
    getToolData(toolname) {
        let toolData = {
            action: "tools",
            city: this.data.city,
            keywords: toolname,
            version: app.globalData.version,
            type: "channel"
        };
        util.request('service.php', toolData, ).then(res => {
            this.data.wendaData.forEach((v, k) => {
                if (toolname == v.wapname) {
                    this.data.wendaData[k].tool = res.data.data.slice(0, 4);
                }
            })
            this.setData({
                wendaData: this.data.wendaData
            })
        })
    },

    // 栏目点击事件
    lanmuTap(e) {
        let id = e.currentTarget.dataset.id;
        let type = e.currentTarget.dataset.type;
        // let name = e.currentTarget.dataset.name;
        wx.navigateTo({
            url: '../article/list/list?id=' + id + '&type=' + type + '&city=' + this.data.citycode,
        })
    },

    // 工具、服务点击事件
    servBtn(e) {
        let path = e.currentTarget.dataset.path;
        let appid = e.currentTarget.dataset.appid;
        let linkname = e.currentTarget.dataset.title;
        // this.data.item.path = path;

        this.setData({
            path,
            linkname,
            // item,
        })
        if (path.indexOf("pages") == -1 && path.indexOf("mp.weixin.qq.com") == -1) {
            util.navContact(linkname, path)
            this.setData({
                showtk: true
            })
        } else {
            util.navMethod(path, appid);
        }
    },

    // 进入客服会话
    contact() {
        this.setData({
            showtk: false,
        })
        let replay = {
            action: "reply",
            title: this.data.linkname,
            url: this.data.path,
            openid: wx.getStorageSync("openid")
        }
        util.request('user.php', replay, 'POST')
    },

    // 关闭
    close() {
        this.setData({
            showtk: false,
        })
    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function() {

    },


    // 分享朋友圈
    onShareTimeline: function() {
        return {

            query: { id: this.data.id, type: this.data.type, city: this.data.citycode },
        }
    },

})